const hasClsNameStr = (node, clsName) => {
  if (!node) return false;

  const clsNameStr = node.classList.toString();

  return ~clsNameStr.indexOf(clsName);
};

export const isClose = (node1, node2) => {
  if (!node1 || !node2) return false;

  return node1.nextSibling === node2 || node2.nextSibling === node1;
};
export const isColumnpanelEle = node => hasClsNameStr(node, 'f-columnpanel');
export const isDroppableEle = node => hasClsNameStr(node, 'wea-edc-droppable');

let store = {
  y: 0,
  isDroppable: false,
  isFromTopToBottom: false
};

export const onMove = evt => {
  const { dragged, related, originalEvent: { pageY } } = evt;
  const isDroppable = isDroppableEle(related);

  if (isDroppable) {
    store.isDroppable = true;
    store.y = pageY;
    return;
  }
  // 当从上而下拖至droppable中后，5为阈值，再次拖动5px则视为正常拖动，不再阻止聚焦在布局组件中
  if (store.isFromTopToBottom && Math.abs(pageY - store.y) > 5) {
    store.y = pageY;
    store.isFromTopToBottom = false;
    return;
  }
  // 优化，将非布局组件从上而下拖到布局组件时无法聚焦到droppable中
  if (store.isDroppable && isDroppableEle(dragged.parentNode)) {
    store.y = pageY;
    store.isFromTopToBottom = true;
    return false;
  }

  store.isDroppable = false;

  // 聚焦至布局组件
  if (isClose(dragged, related) && isColumnpanelEle(related)) {
    const droppables = related.querySelectorAll('.wea-edc-droppable');

    if (!droppables.length) return;

    return false;
  }
}